<template>
	<view class="user-auth-app">
		<view class="img-container">
			<image src="../../../static/user_center.png" mode="aspectFit" class="img" />
		</view>
		<view class="tip-h1">申请获取以下权限</view>
		<view class="tip-text">获取您的公开信息(昵称，头像，地区等)</view>
		<!--
		<view class="submit-btn" @click="auth">授权登录</view>
		-->
		<!--
		<button open-type="getUserInfo" @getuserinfo="bindGetUserInfo" class="submit-btn" hover-class="hover">授权登录</button>
		-->
		<button @click="getUserProfile" class="submit-btn" hover-class="hover">授权登录</button>
		
	</view>
</template>

<script>
	import util from "@/libs/util.js"
	
	export default {
		data() {
			return {
				
			}
		},
		onReady() {
			global.loginByCode()
		},
		onSow() {
			console.log("auth on show");
			global.loginByCode()
		},
		methods: {
			...util,
			auth() {
				this.$Message.success('登录成功')
			},
			getUserProfile() {
				wx.getUserProfile({
					desc: '用于完善会员资料',
					success: (res) => {
						//console.log(res);
						//res.userInfo;
						let e = {detail: res};
						this.submit(e);
					},
					fail: (res) => {
						this.$Message.error('获取用户信息失败');
					}
				});
			},
			bindGetUserInfo(e) {
				if (e.detail.userInfo) {
					this.submit(e)
				} else {
					this.$Message.error('用户信息不存在')
				}
			},
			submit(e) {

				var wxToken = uni.getStorageSync('wx_token');
				uni.showLoading({
					title: '登录中'
				});
				
				this.$api.post('/app/user/auth/wxMplogin', {
					code: global.getWxCode(),
					rawData: e.detail.rawData,
					signature: e.detail.signature,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					userInfo: e.detail.userInfo
				}).then((response) => {
					if (response.code == 0) {
						global.setToken(response.data.token)
						this.$store.commit('setGlobalToken', response.data.token)
						//this.$emit('setGlobalUserInfo', response.data)
						//this.$store.commit('setGlobalUserInfo', response.data)
						this.$Message.success('登录成功')
						
						uni.navigateBack({
							delta: 1
						});
						
					} else {
						this.$Message.error(response.msg)
					}
					uni.hideLoading()
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.user-auth-app{
		font-size:32upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.img-container{
			background:#d4e1fa;
			width: 200upx;
			height:200upx;
			display: flex;
			justify-content: center;
			margin-top:50upx;
			.img{
				width: 200upx;
				height: 150upx;
				margin-top:20upx;
			}
		}

		.tip-h1{
			color:#37403d;
			margin: 30upx 0;
		}
		.tip-text{
			color:#a7a7a7;
			margin-bottom:30upx;
		}
		.submit-btn{
			background-color: #1D2C6D;
			/*padding: 30upx 30upx;*/
			color:white;
			width:80%;
			text-align: center;
		}
	}
</style>
